package com.redare.dev.smart.generator.service.dao;

import com.redare.dev.smart.generator.sdk.pojo.OnlFormHead;
import com.redare.dev.smart.generator.sdk.pojo.form.OnlFormHeadForm;
import com.redare.devframework.common.pojo.Page;
import com.redare.devframework.sql.Sql;
import com.redare.devframework.sql.db.base.SpringJdbcHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * DAO
 *
 * @date 2021-08-28 11:44:05
 * Created by yangqiangyu
 */
@Repository(value = "com.redare.dev.smart.generator.dao.OnlFormHeadDao")
public class OnlFormHeadDao {

    @Autowired
    SpringJdbcHelper jdbcHelper;

    public long addOnlFormHead(OnlFormHeadForm.Add form) {
        Sql sql = Sql.insert("onl_form_head")
                .field("sys_id", form.getSysId())
                .field("table_name", form.getTableName())
                .field("table_type", form.getTableType())
                .field("tableVersion", form.getTableVersion())
                .field("table_txt", form.getTableTxt())
                .field("is_checkbox", form.getIsCheckbox())
                .field("is_db_synch", form.getIsDbSynch())
                .field("is_page", form.getIsPage())
                .field("is_tree", form.getIsTree())
                .field("id_sequence", form.getIdSequence())
                .field("id_type", form.getIdType())
                .field("query_mode", form.getQueryMode())
                .field("relation_type", form.getRelationType())
                .field("sub_table_str", form.getSubTableStr())
                .field("tab_order_num", form.getTabOrderNum())
                .field("tree_parent_id_field", form.getTreeParentIdField())
                .field("tree_id_field", form.getTreeIdField())
                .field("tree_field_name", form.getTreeFieldname())
                .field("form_category", form.getFormCategory())
                .field("form_template", form.getFormTemplate())
                .field("form_template_mobile", form.getFormTemplateMobile())
                .field("scroll", form.getScroll())
                .field("copy_version", form.getCopyVersion())
                .field("copy_type", form.getCopyType())
                .field("physic_id", form.getPhysicId())
                .field("ext_config_json", form.getExtConfigJson())
                .field("update_by", form.getUpdateBy())
                .field("update_time", form.getUpdateTime())
                .field("create_by", form.getCreateBy())
                .field("theme_template", form.getThemeTemplate())
                .field("is_des_form", form.getIsDesForm())
                .field("des_form_code", form.getDesFormCode())
                .field("create_time", new Date());
        return jdbcHelper.insertReturnFieldNumber(sql).longValue();
    }


    public long addOnlFormHeadByBean(OnlFormHead form) {
        Sql sql = Sql.insert("onl_form_head")
                .field("sys_id", form.getSysId())
                .field("table_name", form.getTableName())
                .field("table_type", form.getTableType())
                .field("sub_path", form.getSubPath())
                .field("table_version", 1)
                .field("comment", form.getComment())
                .field("is_checkbox", form.getIsCheckbox())
                .field("is_db_synch", form.getIsDbSynch())
                .field("page", form.isPage())
                .field("tree", form.isTree())
                .field("id_sequence", form.getIdSequence())
                .field("id_type", form.getIdType())
                .field("query_mode", form.getQueryMode())
                .field("relation_type", form.getRelationType())
                .field("sub_table_str", form.getSubTableStr())
                .field("tab_order_num", form.getTabOrderNum())
                .field("tree_parent_id_field", form.getTreeParentIdField())
                .field("tree_id_field", form.getTreeIdField())
                .field("tree_fieldname", form.getTreeFieldname())
                .field("form_category", form.getFormCategory())
                .field("form_template", form.getFormTemplate())
                .field("form_template_mobile", form.getFormTemplateMobile())
                .field("scroll", form.getScroll())
                .field("copy_version", form.getCopyVersion())
                .field("copy_type", form.getCopyType())
                .field("physic_id", form.getPhysicId())
                .field("ext_config_json", form.getExtConfigJson())
                .field("update_by", form.getUpdateBy())
                .field("update_time", form.getUpdateTime())
                .field("create_by", form.getCreateBy())
                .field("theme_template", form.getThemeTemplate())
                .field("is_des_form", form.getIsDesForm())
                .field("des_form_code", form.getDesFormCode())
                .field("create_time", new Date());
        return jdbcHelper.insertReturnFieldNumber(sql).longValue();
    }

    public int[] addOnlFormHead(List<OnlFormHeadForm.Add> list) {
        if (list == null || list.isEmpty()) {
            return new int[]{};
        }
        Sql sql = Sql.insert("onl_form_head")
                .fields("sys_id", "table_name", "table_type", "table_version", "table_txt", "is_checkbox", "is_db_synch", "is_page", "is_tree", "id_sequence", "id_type", "query_mode", "relation_type", "sub_table_str", "tab_order_num", "tree_parentId_field", "tree_id_field", "tree_fieldname", "form_category", "form_template", "form_template_mobile", "scroll", "copy_version", "copy_type", "physic_id", "ext_config_json", "update_by", "update_time", "create_by", "create_time", "theme_template", "is_des_form", "des_form_code");
        Date createTime = new Date();
        List<Object[]> params = new ArrayList<>();
        for (OnlFormHeadForm.Add item : list) {
            params.add(new Object[]{item.getSysId(), item.getTableName(), item.getTableType(), item.getTableVersion(), item.getTableTxt(), item.getIsCheckbox(), item.getIsDbSynch(), item.getIsPage(), item.getIsTree(), item.getIdSequence(), item.getIdType(), item.getQueryMode(), item.getRelationType(), item.getSubTableStr(), item.getTabOrderNum(), item.getTreeParentIdField(), item.getTreeIdField(), item.getTreeFieldname(), item.getFormCategory(), item.getFormTemplate(), item.getFormTemplateMobile(), item.getScroll(), item.getCopyVersion(), item.getCopyType(), item.getPhysicId(), item.getExtConfigJson(), item.getUpdateBy(), item.getUpdateTime(), item.getCreateBy(), createTime, item.getThemeTemplate(), item.getIsDesForm(), item.getDesFormCode()});
        }
        return jdbcHelper.batchUpdate(sql, params);
    }


    public int[] addOnlFormHeadByBean(List<OnlFormHead> list) {
        if (list == null || list.isEmpty()) {
            return new int[]{};
        }
        Sql sql = Sql.insert("onl_form_head")
                .fields("sys_id", "table_name", "table_type", "table_version", "comment", "is_checkbox", "is_db_synch", "page", "tree", "id_sequence", "id_type", "query_mode", "relation_type", "sub_table_str", "tab_order_num", "tree_parent_id_field", "tree_id_field", "tree_fieldname", "form_category", "form_template", "form_template_mobile", "scroll", "copy_version", "copy_type", "physic_id", "ext_config_json", "update_by", "update_time", "create_by", "create_time", "theme_template", "is_des_form", "des_form_code");
        Date createTime = new Date();
        List<Object[]> params = new ArrayList<>();
        for (OnlFormHead item : list) {
            params.add(new Object[]{item.getSysId(), item.getTableName(), item.getTableType(), item.getTableVersion(), item.getComment(), item.getIsCheckbox(), item.getIsDbSynch(), item.isPage(), item.isTree(), item.getIdSequence(), item.getIdType(), item.getQueryMode(), item.getRelationType(), item.getSubTableStr(), item.getTabOrderNum(), item.getTreeParentIdField(), item.getTreeIdField(), item.getTreeFieldname(), item.getFormCategory(), item.getFormTemplate(), item.getFormTemplateMobile(), item.getScroll(), item.getCopyVersion(), item.getCopyType(), item.getPhysicId(), item.getExtConfigJson(), item.getUpdateBy(), item.getUpdateTime(), item.getCreateBy(), createTime, item.getThemeTemplate(), item.getIsDesForm(), item.getDesFormCode()});
        }
        return jdbcHelper.batchUpdate(sql, params);
    }

    public int deleteOnlFormHead(OnlFormHeadForm.Del form) {
        Sql sql = Sql.delete("onl_form_head")
                .where().andEq("id", form.getId());
        return jdbcHelper.update(sql.toString(), sql.getParams().toArray());
    }

    public Page<OnlFormHead> findOnlFormHeadPage(OnlFormHeadForm.Search form, int curPage, int pageSize, boolean calCount, boolean allList) {
        Sql sql = Sql.select()
                .fieldsIf(StringUtils.isNotBlank(form.getQueryFields()), form.getQueryFields())
                .fieldsIf(StringUtils.isBlank(form.getQueryFields()), "*")
                .from("onl_form_head")
                .where()
                .andBLikeFieldsJoinByOrIfNotBlank(form.getKey()
                        , "table_name","comment"
                )
                .andEqIfNotNull("sysId", form.getSysId())
                .andEqIfNotBlank("table_name",form.getTableName())
                .andInIfNotEmpty("table_name",form.getTableNames())
                .andEqIfNotNull("tableType", form.getTableType())
                .orderBy().desc("id");
        return jdbcHelper.queryForPageBean(sql.toString(), OnlFormHead.class, sql.getParams().toArray(), curPage, pageSize, calCount, allList);
    }


    public int editOnlFormHead(OnlFormHeadForm.Edit form) {
        Sql sql = Sql.update("onl_form_head")
                .setIfNotNull("sys_id", form.getSysId())
                .setIfNotBlank("table_name", form.getTableName())
                .setIfNotBlank("sub_path", form.getSubPath())
                .setIfNotBlank("comment", form.getComment())
                .setIfNotNull("table_type", form.getTableType())
                .setIfNotNull("table_version", form.getTableVersion())
                .setIfNotBlank("table_txt", form.getTableTxt())
                .setIfNotBlank("is_checkbox", form.getIsCheckbox())
                .setIfNotBlank("is_db_synch", form.getIsDbSynch())
                .setIfNotNull("page", form.getPage())
                .setIfNotNull("tree", form.getTree())
                .setIfNotBlank("id_sequence", form.getIdSequence())
                .setIfNotBlank("id_type", form.getIdType())
                .setIfNotBlank("query_mode", form.getQueryMode())
                .setIfNotNull("relation_type", form.getRelationType())
                .setIfNotBlank("sub_table_str", form.getSubTableStr())
                .setIfNotNull("tab_order_num", form.getTabOrderNum())
                .setIfNotBlank("tree_parent_id_field", form.getTreeParentIdField())
                .setIfNotBlank("tree_id_field", form.getTreeIdField())
                .setIfNotBlank("tree_fieldname", form.getTreeFieldname())
                .setIfNotBlank("form_category", form.getFormCategory())
                .setIfNotBlank("form_template", form.getFormTemplate())
                .setIfNotBlank("form_template_mobile", form.getFormTemplateMobile())
                .setIfNotNull("scroll", form.getScroll())
                .setIfNotNull("copy_version", form.getCopyVersion())
                .setIfNotNull("copy_type", form.getCopyType())
                .setIfNotBlank("physic_id", form.getPhysicId())
                .setIfNotBlank("ext_config_json", form.getExtConfigJson())
                .setIfNotBlank("update_by", form.getUpdateBy())
                .setIfNotNull("update_time", form.getUpdateTime())
                .setIfNotBlank("create_by", form.getCreateBy())
                .setIfNotNull("create_time", form.getCreateTime())
                .setIfNotBlank("theme_template", form.getThemeTemplate())
                .setIfNotBlank("is_des_form", form.getIsDesForm())
                .setIfNotBlank("des_form_code", form.getDesFormCode())
                .setIfNotNull("modalForm", form.getModalForm())
                .setIfNotNull("imported", form.getImported())
                .setIfNotNull("exportExcel", form.getExportExcel())
                .setIfNotNull("formColumnCount", form.getFormColumnCount())
                .where()
                .andEq("id", form.getId());
        return jdbcHelper.update(sql.toString(), sql.getParams().toArray());
    }

    public OnlFormHead getOnlFormHead(OnlFormHeadForm.Get form) {
        Sql sql = Sql.select()
                .fieldsIf(StringUtils.isNotBlank(form.getQueryFields()), form.getQueryFields())
                .fieldsIf(StringUtils.isBlank(form.getQueryFields()), "*")
                .from("onl_form_head")
                .where()
                .andEqIfNotNull("table_name",form.getTableName())
                .andEqIfNotNull("table_type",form.getTableType())
                .andEqIfNotNull("id", form.getId());
        return jdbcHelper.queryForBean(sql.toString(), OnlFormHead.class, sql.getParams().toArray());
    }


}

